package com.Calculator.view.Frame.impl;

import com.Calculator.controller.DisplayText.KeyListener;
import com.Calculator.models.impl.Theme;
import com.Calculator.view.Panel.impl.ButtonPanel;
import com.Calculator.view.Panel.impl.TextPanel;
import com.Calculator.view.Frame.iMyJFrame;

import javax.swing.*;
import java.awt.*;

public class CalculatorFrame implements iMyJFrame {
    //计算器窗口
    private JFrame calculatorFrame;
    //文本面板组件
    private TextPanel textPanel;
    //按钮面板组件
    private ButtonPanel buttonPanel;
    // 键盘监听器
    KeyListener keyListener;

    public CalculatorFrame(Theme theme) {
        //计算器框架
        calculatorFrame = new JFrame();
        //设置布局管理器
        calculatorFrame.setLayout(new BorderLayout());
        //设置大小
        calculatorFrame.setSize(theme.getWidth(), theme.getHeight());
        //设置标题
        calculatorFrame.setTitle("计算器");
        //设置关闭模式
        calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置置顶
        calculatorFrame.setAlwaysOnTop(true);
        //设置居中显示
        calculatorFrame.setLocationRelativeTo(null);
        //设置背景颜色
        calculatorFrame.setBackground(Color.blue);

        //添加文本面板组件
        textPanel = new TextPanel(theme);
        buttonPanel = new ButtonPanel(theme);
        calculatorFrame.add(textPanel.getJPanel(),BorderLayout.NORTH);
        calculatorFrame.add(buttonPanel.getJPanel(),BorderLayout.CENTER);

        //不可改变窗口大小
        calculatorFrame.setResizable(false);
        //自适应大小
        calculatorFrame.pack();
        //显示窗口
        calculatorFrame.setVisible(true);


        //添加监听器
        keyListener = new KeyListener(textPanel);
        textPanel.getJPanel().addKeyListener(keyListener);
    }

    public TextPanel getTextPanel() {
        return textPanel;
    }

    public ButtonPanel getButtonPanel() {
        return buttonPanel;
    }

    @Override
    public JFrame getJFrame() {
        return calculatorFrame;
    }

    @Override
    public void setJFrame(JFrame jFrame) {
        this.calculatorFrame = jFrame;
    }
}
